// pages/catUser/catUser.js
import request from "../../utils/request";
import util from "../../utils/util"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    userPlayList: {},
    navState: 0,
    userAccount: {},
    userLike: {}, //喜欢的音乐
    userCreate: [], //创建的歌单
    userCollect: [], //收藏的歌单
    userComment: [], //用户历史评论
    userEvent: [], //用户动态
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let userId = options.id
    this.getUserInfo(userId)
    this.getPlayList(userId)
    // this.getComment(userId)
    this.getUserEvent(userId)
  },  

  async getUserInfo(id) {
    let userInfo = await request("/user/detail", {uid: id})
    userInfo.createDay = Math.floor(userInfo.createDays/365) //村龄
    
    var registDay = new Date(userInfo.createTime);
    userInfo.registDay = util.formatDate(registDay) //注册日期

    var birthDay = new Date(userInfo.profile.birthday);
    userInfo.birthDay = util.formatDate(birthDay) //注册日期

    this.setData({
      userInfo
    })
    
  },

  //用户歌单
  async getPlayList(id) {
    let userPlayList = await request("/user/playlist",{uid: id})
    userPlayList = userPlayList.playlist
    console.log(userPlayList)
    let userLike = userPlayList[0]
    let userCreate = userPlayList.filter(item => {
      return item.userId == id
    })
    let userCollect = userPlayList.filter(item => {
      return item.userId != id
    })

    this.setData({
      userLike,
      userCreate,
      userCollect
    })

  },

  // async getComment(id) {
  //   let userComment = await request("/user/comment/history",{uid: id})
  //   console.log(userComment)
  // },

  async getUserEvent(id) {
    let userEvent = await request("/user/event",{uid: id})
    userEvent = userEvent.events
    userEvent.forEach(item => {
      item.json = JSON.parse(item.json)
    })
    userEvent.forEach(item => {
      let date = new Date(item.eventTime)
      item.eventTime = util.formatDate(date)
    })
    this.setData({
      userEvent
    })
    console.log(this.data.userEvent)
  },

  navSwitch: function (e) {
    // console.log(e.currentTarget.dataset.index)
    let index = e.currentTarget.dataset.index;
    this.setData({
      navState: index
    })
  },

  bindchange(e) {
    // console.log(e.detail.current)
    let index = e.detail.current;
    this.setData({
      navState: index
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})